package com.artezio.task.model;

import com.artezio.task.dto.FormatDataDto;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Collections;
import java.util.*;

import java.util.Set;

import static javax.persistence.GenerationType.IDENTITY;

@Entity
@Table(name = "contact")
public class Contact implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private int id;
    private int version;
    private String firstName;
    private String lastName;
    //private Date birthDate;
    private List<ContactTelDetail> contactTelDetails = new LinkedList<ContactTelDetail>();
//	private Set<Hobby> hobbies = new HashSet<Hobby>();

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "ID")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Version
    @Column(name = "VERSION")
    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    @Column(name = "FIRST_NAME")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name = "LAST_NAME")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /*
@Temporal(TemporalType.DATE)
@Column(name = "BIRTH_DATE")
public Date getBirthDate() {
return birthDate;
}

public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}

public String toString() {
return "Contact - Id: " + id + ", First name: " + firstName
        + ", Last name: " + lastName + ", Birthday: " + birthDate;
}
    */
    @OneToMany(mappedBy = "contact", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
//    @Cascade(CascadeType.ALL)
	@Fetch(FetchMode.SELECT)
    @BatchSize(size = 10)
    public List<ContactTelDetail> getContactTelDetails() {
        return contactTelDetails;
    }

    public void setContactTelDetails(List<ContactTelDetail> contactTelDetails) {
        this.contactTelDetails = contactTelDetails;
    }

    public void addContactTelDetail(ContactTelDetail contactTelDetail) {
        contactTelDetail.setContact(this);
        getContactTelDetails().add(contactTelDetail);
    }

    public void removeContactTelDetail(ContactTelDetail contactTelDetail) {
        getContactTelDetails().remove(contactTelDetail);
    }

    public void setDataFromDto(FormatDataDto dto) {
        firstName = dto.getFirstName();
        lastName = dto.getLastName();
//        if ((dto.getNumberOne()!=null)&&(dto.getTypeNumOne()!=null)&&(!dto.getNumberOne().isEmpty()) && (!dto.getTypeNumOne().isEmpty())) {
//            ContactTelDetail det1 = new ContactTelDetail();
//            det1.setContact(this);
//            det1.setTelNumber(dto.getNumberOne());
//            det1.setTelType(dto.getTypeNumOne());
//            this.addContactTelDetail(det1);
//        }

        if (dto.getNumberTwo() != null && dto.getTypeNumTwo() != null && !dto.getNumberTwo().isEmpty() && !dto.getTypeNumTwo().isEmpty()) {
            ContactTelDetail det2 = new ContactTelDetail();
            det2.setContact(this);
            det2.setTelNumber(dto.getNumberTwo());
            det2.setTelType(dto.getTypeNumTwo());
            this.addContactTelDetail(det2);
        }
        if (!dto.getContactTelDetails().isEmpty() && dto.getContactTelDetails() != null) {
            contactTelDetails = (List<ContactTelDetail>) dto.getContactTelDetails();
        }


    }
//	@ManyToMany
//	@JoinTable(name = "contact_hobby_detail", joinColumns = @JoinColumn(name = "CONTACT_ID"), inverseJoinColumns = @JoinColumn(name = "HOBBY_ID"))
//	public Set<Hobby> getHobbies() {
//		return hobbies;
//	}

    //	public void setHobbies(Set<Hobby> hobbies) {
//		this.hobbies = hobbies;
//	}
    public void sortDets() {
        Collections.sort(contactTelDetails);
    }

    public String allDets() {
        String dets = "";
        for (ContactTelDetail ctd : contactTelDetails) {
            dets = dets + ctd.getTelType() + "-" + ctd.getTelNumber() + " ";
        }
        return dets;
    }
}
